;
(function() {
	var skill = {
		save: function() {
			db.update(config.dbname.cardFactory, this._id, this);
		},
		destroy: function() {
			db.remove(config.dbname.cardFactory, this._id);
		},
		setInfo: function(info) {
			var self = this;
			//名称
			var name = String(info.name || "【会员卡】").trim();
			name = name.substr(0, 20); //限制不能超过20个字符
			if (!helper.folder_name(name)) {
				throwCusError("会员卡名不规范")
			}
			this.info.name = name;

			//可领取几张
			var card_list_limit = +info.card_list_limit || Number.MAX_VALUE;
			this.info.card_list_limit = card_list_limit;

			//过期时间
			var diedline = +info.diedline || Number.MAX_VALUE;
			this.info.diedline = diedline;

			//类型
			var type = +info.type;
			if (!type && CardFactoryCon.TYPE.hasOwnValue(type)) {
				throwCusError("找不到指定会员卡类型")
			}
			this.info.type = type;
			//类型对应的相关配置
			//打折：折扣比例，是否有相关梯度
			//满减：满多少，减多少，是否有相关梯度
			//抵用：可抵用的现金额度，是否有相关梯度
			var config = info.config || {};
			this.info.config = config;

			//适用的范围
			//所有商品+卡专属商品
			var bus = BusCon.get(this.bus_id);
			//指定商品适用
			var goods_limit = Array.isArray(info.goods_limit) ? info.goods_limit : null;
			this.info.goods_limit = goods_limit && info.goods_limit.filter(function(goods_id) {
				return bus.hasGoodsById(goods_id);
			});
			//指定标签适用
			var classify_limit = Array.isArray(info.classify_limit) ? info.classify_limit : null;
			this.info.classify_limit = classify_limit && info.classify_limit.filter(function(tag_id) {
				return self.hasClassifyById(tag_id);
			});

			//描述，使用官方固定描述
			this.info.intro = this.getIntro();

			//自定义描述
			var custom_info = String(info.custom_info || "").trim();

			//领取模式
			this.info.need_buy = helper.boolean_parse(info.need_buy);
			this.info.need_buy_price = +info.need_buy_price || 0;
			this.info.need_buy_back_cash = +info.need_buy_back_cash || 0;
			if (this.info.need_buy) {
				var error_msg = []
				if (!bus.getPerm().can_set_goods_call_back_buff) { //需要商品支付回调的权限
					error_msg.push("【商品支付回调】");
				}
				if (!bus.getPerm().can_sale_virtual_goods) {
					error_msg.push("【虚拟商品交易】");
				}
				if (error_msg.length) {
					throwCusError("需要" + error_msg.join("；") + "的权限")
					return
				}
				var need_buy_goods_info = {
					goods_name: this.info.name,
					num: Number.MAX_VALUE,
					intro: this.info.intro,
					price: this.info.need_buy_price,
					back_cash: this.info.need_buy_back_cash,
					is_virtual: true //虚拟商品
				};
				if (!this.info.buy_goods_id) {
					// 初始化商品
					var buy_goods = new GoodsCon(need_buy_goods_info, this.bus_id);
					// 初始化商品支付回调
					bus.setGoodsEndPayCallbackBuff(buy_goods._id, "http://127.0.0.1:3000/user/card/add_card_from_card_factory/need_buy_cb/" +
						this._id +
						"?ach_id={ach_id}" +
						"&user_id={user_id}");
					this.info.buy_goods_id = buy_goods._id;
				} else {
					var buy_goods = GoodsCon.get(this.info.buy_goods_id)
				}
				buy_goods._setInfo(need_buy_goods_info);
			}
			this.info.custom_info = custom_info;
		},
		getIntro: function() {
			return CardFactoryCon.getIntro(this);
		},
		hasCard_ByUserId: function(user_id) {
			return this.card_list.some(function(card_id) {
				return CardFactoryCon.cardId_has_user(card_id, user_id)
			});
		},
		//生成针对某个用户的会员卡
		toCard: function(user_id) {
			if (this.hasCard_ByUserId(user_id)) {
				throwCusError("指定用户已经有此会员卡，不可重复领取");
			} else {
				var card = new CardCon({
					user_id: user_id,
					bus_id: this.bus_id,
					card_factory_id: this._id
				});
				this.card_list.push(this._id);
				this.save();
			}
			return card;
		},
	};
	module.exports = skill.multiInherits(
		require("./skill.goods"),
		require("../bus/skill.classify")
	);

	var db = require("../db/index");
	var CardCon = require("../card/_con");
	var CardFactoryCon = require("../card_factory/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
}());